//
//  BC_BrowserTreeNode.m
//  GameTree
//
//  Created by BrunoCausse on 13/11/10.
//  Copyright 2010-2013 poulailler cie. All rights reserved.
//

#import "BC_BrowserTreeNode.h"
#import "BC_Move.h"
#import "BC_Board.h"

NSString * const BCInfosKey		= @"moveInfos";
NSString * const BCChildrenKey	= @"children";



@implementation BC_BrowserTreeNode

#pragma mark init/dealloc

- (id)init
{
	self = [super init];
	if (self) {
	}
	return self;
}

+ (id)treeNodeWithRepresentedObject:(id)modelObject
{
	return [super treeNodeWithRepresentedObject:modelObject];
}

- (void)dealloc
{
	[super dealloc];
}
	
			
	


/*
 creer un node vide
 */

+ (BC_BrowserTreeNode *)treeNodeEmpty
{
	BC_Move *move = [[[BC_Move alloc] init] autorelease];
	move.title = @"root";
	return [BC_BrowserTreeNode treeNodeWithRepresentedObject:move];; //autorealease
}

/*
 creer une arborescence a partir d'une property list
 
 doit rejouer tous les coup
 */


+ (BC_BrowserTreeNode *)nodeWithPropertyList:(NSDictionary *)dictionary withBoard:(BC_Board *)board
{
	BC_Move *move = [BC_Move moveWithContents:[dictionary valueForKey:BCInfosKey]];
	
	int nFlips = -1;
	
	if(![move.title isEqualToString:@"root"]) {
		nFlips = [board generateMove:move];
		
		if(nFlips) {
			[board doMove:move];
		} else {
			[board doPass];
		}
	}
	
	BC_BrowserTreeNode *node = [BC_BrowserTreeNode treeNodeWithRepresentedObject:move];
	NSArray *children = [dictionary valueForKey:BCChildrenKey];
	if (children != nil) {
		for(NSDictionary *dict in children) {
			BC_BrowserTreeNode *child = [BC_BrowserTreeNode nodeWithPropertyList:dict withBoard:board];
			[[node mutableChildNodes] addObject:child];
		}
	}


	if(nFlips > 0) {
		[board undoMove:move];
	} else if (nFlips == 0) {
		[board doPass];
	}
		
	return node;
	
}

/*
 cree un dictionnaire avec l'arborescence du noeud
 */

- (NSDictionary *)propertyList
{
	NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithCapacity:2];
	[dictionary setObject:[[self representedObject] contents] forKey:BCInfosKey];
	
	if (![self isLeaf]) {
		/* 
		 idem avec les enfants (recursif)
		 */
		NSMutableArray * childrenAsPlist;
		childrenAsPlist = [NSMutableArray arrayWithCapacity:[[self childNodes] count]];
		for(BC_BrowserTreeNode *node in [self childNodes]) {
			[childrenAsPlist addObject:[node propertyList]];
		}
		[dictionary setObject:childrenAsPlist forKey:BCChildrenKey];
	}
	
	
	return dictionary;
}


/*
 retourne l'image corespondant a l'information du noeud
 */

//- (NSImage *)image
//{
//	int imageId = [[[self representedObject] valueForKey:BCImageIDKey] intValue];
//	switch (imageId) {
//		case 0:
//			return [NSImage imageNamed:@"flagged"];
//		case 1:
//			return [NSImage imageNamed:NSImageNameAddTemplate];
//		case 2:
//			return [NSImage imageNamed:NSImageNamePathTemplate];
//			
//		default:
//			return nil;
//	}
//}


//#pragma mark Walking
//
//- (NSIndexPath *)firstNodeWithTitlePrefix:(NSString *)prefix
//{
//	NSString *title = [[[self representedObject] valueForKey:BCTitleKey] lowercaseString];
//	if ([title hasPrefix:[prefix lowercaseString]]) {
//		return [self indexPath];										//trouvé retourne indexPath
//	}
//	
//	//recursif
//	for(BC_BrowserTreeNode *node in [self childNodes]) {
//		NSIndexPath *indexPath = [node firstNodeWithTitlePrefix:prefix];
//		if([indexPath length] != 0)
//			return indexPath;									//retour recursif si trouvé
//	}
//	
//	return [NSIndexPath indexPathWithIndexes:NULL length:0];	//pas de reponse indexPath NULL
//}

@end
